package com.yuhang.security.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yuhang.security.dto.FileInfo;

/**
 * 上传和下载
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/file")
public class FileController {

	@PostMapping
	public FileInfo file(MultipartFile file) throws IllegalStateException, IOException{
		System.out.println("into file");
		System.out.println(file.getName());
		System.out.println(file.getOriginalFilename());
		System.out.println(file.getSize());
		
		File localFile = new File(new Date().getTime()+".txt");
		file.transferTo(localFile);
		System.out.println("localFile.getAbsolutePath()"+localFile.getAbsolutePath());
		return new FileInfo(localFile.getAbsolutePath());
	}
	
	/*@GetMapping("/{id}")
	public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) throws Exception {

		try (InputStream inputStream = new FileInputStream(new File(folder, id + ".txt"));
				OutputStream outputStream = response.getOutputStream();) {
			
			response.setContentType("application/x-download");
			response.addHeader("Content-Disposition", "attachment;filename=test.txt");
			
			IOUtils.copy(inputStream, outputStream);
			outputStream.flush();
		} 

	}*/
}
